package logic

import (
	"context"
	"errors"
	"user_srv/model/mysql"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *user.RegisterRequest) (*user.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{
		Username: in.Username,
		Password: in.Password,
	}
	err := u.UserCreate()
	if err != nil {
		logx.Error("用户注册失败")
		return nil, errors.New("注册失败")
	}
	return &user.RegisterResponse{Id: int64(u.ID)}, nil
}
